package com.baibei.ebec.totrade;

import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baibei.ebec.home.R;
import com.baibei.model.ProductInfo;
import com.blankj.utilcode.utils.LogUtils;
import com.blankj.utilcode.utils.Utils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class ToTradeGoodsAdapter extends RecyclerView.Adapter<ToTradeGoodsAdapter.ViewHolder> {

    private ArrayList<ProductInfo> datas = null;

    private ArrayList<Double> lastPrices;

    public ToTradeGoodsAdapter(ArrayList<ProductInfo> list) {
        this.datas = list;
    }

    public int bg;

    @Override
    public ToTradeGoodsAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_totrade_goods, null);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ToTradeGoodsAdapter.ViewHolder viewHolder, final int i) {
        ProductInfo messageInfo = datas.get(i);
        LogUtils.e("tags5", "getLastPrice" + messageInfo.getLastPrice() + "getClosePrice" + messageInfo.getClosePrice());
        double scope = 0;
        if (lastPrices == null){
            scope = (messageInfo.getLastPrice() - Double.parseDouble(messageInfo.getClosePrice())) / 100.00d;
        } else {
            scope = (lastPrices.get(i) - Double.parseDouble(messageInfo.getClosePrice())) / 100.00d;
        }

        Log.e("scopes",scope+"");
        int textColorResource;
        int roseBgId = 0;
        LogUtils.e("tags5", scope);
        if (scope > 0) {
            roseBgId = R.drawable.arrow_red;
            textColorResource = R.color.buyColor;
        }
        // 下跌
        else if (scope < 0) {
            roseBgId = R.drawable.arrow_green;
            textColorResource = R.color.sellColor;
        }
        // 平稳
        else {
            textColorResource = R.color.rgb666666;
        }
        if (roseBgId == 0) {
            viewHolder.img_arrow.setImageResource(0);
        } else {
            viewHolder.img_arrow.setImageResource(roseBgId);
        }
        viewHolder.tv_name.setText(messageInfo.getName());
        viewHolder.tv_dinggou.setText("￥" + messageInfo.getPrice() + "");
        if (lastPrices != null){
            viewHolder.tv_price.setText("￥" + lastPrices.get(i) + "");
        } else {
            viewHolder.tv_price.setText("￥" + messageInfo.getLastPrice() + "");
        }

        viewHolder.tv_price.setTextColor(ResourcesCompat.getColor(Utils.getContext().getResources(), textColorResource, null));
        Glide.with(viewHolder.iv_img).load(messageInfo.getProductPic()).into(viewHolder.iv_img);
        if (messageInfo.isIschecked()) {
            viewHolder.ll_rootview.setBackgroundResource(bg);
        } else {
            viewHolder.ll_rootview.setBackgroundResource(R.drawable.normal_goods_border);
        }
        viewHolder.ll_rootview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mlistener.onitemclick(i);
            }
        });
    }

    public interface ItemClickListener {
        void onitemclick(int pos);
    }

    public void setItemBorder(int i){
        switch (i){
            case 1:
                bg = R.drawable.select_buy_goods_border;
                break;
            case 2:
                bg = R.drawable.select_sell_goods_border;
                break;
        }
    }

    private ItemClickListener mlistener;

    public void setOnitemclickListener(ItemClickListener listener) {
        mlistener = listener;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void setInfos(List<ProductInfo> list) {
        this.datas.addAll(list);
        notifyDataSetChanged();
    }

    public void setLastPrices(ArrayList<Double> lastPrices){
        this.lastPrices = lastPrices;
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_title;
        private final TextView tv_price;
        private final TextView tv_name;
        private final TextView tv_dinggou;
        private final ImageView img_arrow;
        private final ImageView iv_img;
        private final LinearLayout ll_rootview;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_dinggou = itemView.findViewById(R.id.tv_dinggou);
            img_arrow = itemView.findViewById(R.id.img_arrow);
            iv_img = itemView.findViewById(R.id.iv_img);
            ll_rootview = itemView.findViewById(R.id.ll_rootview);
        }
    }
}
